﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using cocos2d;
using CampaignWP7.Base;
using Microsoft.Xna.Framework;

namespace CampaignWP7
{
    public class Balloon:CCSprite
    {
        public float width { set; get; }
        public float height { set; get; }
        public enum BalloonType
        {
            BT_COLOR_RED,
           // BT_COLOR_SPECIAL,
            //
            BT_NUM
        }
        //
        private static string[] BalloonTypeName = { "red", "special" };
        private static int[] BalloonFrameCount = { 2, 1 };
        //string format as :  balloon_red_1.png  1 is the balloonNum
        protected static Dictionary<string, List<CCSpriteFrame>> m_framesDict;
        //
        public enum BalloonState
        {
            BalloonState_Normal = 0,
            BalloonState_Float,
            BalloonState_Exploded,
            //
            BalloonState_Num
        }
        public BalloonType balloonType { set; get; }
        public int BalloonsNum{set;get;}
        public BalloonState State { set; get; }
        public List<CollideCircle> m_BoundingCircles;
        //
        private static int anima_tag = 2;



        public void MoveBounding(Vector2 pos)
        {
            for (int i = 0; i < m_BoundingCircles.Count;++i )
            {
                CollideCircle c = m_BoundingCircles[i];
                c.center = new Vector2(pos.X, pos.Y);
            }
        }
        public void MoveTo(CCPoint pos)
        {
            this.position = new CCPoint(pos.x, pos.y);
            MoveBounding(new Vector2(pos.x, pos.y));
        }
        public Balloon(BalloonType type,int Num)
        {
            //静态初始化
            if (m_framesDict == null || m_framesDict.Count == 0)
            {
                m_framesDict = new Dictionary<string, List<CCSpriteFrame>>();
                for (int i = 0; i < (int)BalloonType.BT_NUM; ++i)
                {
                    List<CCSpriteFrame> temp = new List<CCSpriteFrame>();
                    //---
                    //every animation type shouldn't greater then N!
                    //---
                    for (int j = 1; j <= Num;++j )
                    {
                        for (int k = 0; k < BalloonFrameCount[i]; ++k)
                        {
                            string frameName = BalloonTypeName[i];
                            //the format of name likes  "bullet_normal_1_2.png"
                            //notice: the first 1 is the BalloonsNum
                            //       the scened 2 is the frame counter

                            frameName = "balloon_" + frameName + "_" + j + "_" + k + ".png";
                            CCSpriteFrame spriteframe = CCSpriteFrameCache.sharedSpriteFrameCache().spriteFrameByName(frameName);
                            if (spriteframe != null)
                            {
                                temp.Add(spriteframe);
                            }
                            //
                            if (k == BalloonFrameCount[i] - 1)
                            {
                                if (temp.Count != 0)
                                {
                                    string szName = BalloonTypeName[i] + "_"+j;
                                    m_framesDict.Add(szName, temp);
                                }
                                break;
                            }
                        }
                    }                  
                }
            }
            //
            string initName = "balloon_" + BalloonTypeName[0] + "_" + Num + "_0.png";
            initWithSpriteFrameName(initName);
            this.anchorPoint = new CCPoint(0.5f, 0.0f);
            //
            ResDef.g_BalloonBatchNode.addChild(this);
            CCSpriteFrame frame = CCSpriteFrameCache.sharedSpriteFrameCache().spriteFrameByName(initName);
            width = frame.Rect.size.width;
            height = frame.Rect.size.height;
            State = BalloonState.BalloonState_Normal;
            this.anchorPoint = new CCPoint(0.5f,0.5f);
            //
            m_BoundingCircles = new List<CollideCircle>(Num);
            for (int i = 0; i < Num; ++i)
            {
                CollideCircle c = new CollideCircle(new Vector2(0, 0), width);
                m_BoundingCircles.Add(c);
            }
            //
            ChangeBalloonNum(Num);
        }
        //
        //
        public void PlayFloatAnimation()
        {
            State = BalloonState.BalloonState_Float;
            float high = CCDirector.sharedDirector().getWinSize().height;
            //
            List<CCFiniteTimeAction> acs = new List<CCFiniteTimeAction>();
            CCFiniteTimeAction floataction = CCMoveTo.actionWithDuration(6, new CCPoint(position.x, high+20));
            acs.Add(floataction);
            CCFiniteTimeAction removeaction = CCCallFuncND.actionWithTarget(this, callbackRemoveBalloon, this);
            acs.Add(removeaction);
            CCAction move = CCSequence.actions(acs.ToArray());
            this.runAction(move);
        }
        //
        private void callbackRemoveBalloon(CCNode sender, object data)
        {
            Balloon balloon = data as Balloon;
            if (balloon != null)
            {
                ResDef.g_BalloonBatchNode.removeChild(balloon, true);
            }
        }
        //
        public void ChangeBalloonNum(int n)
        {
            if (BalloonsNum != n)
            {
                BalloonsNum = n;
                //
                if (BalloonsNum <= 0)
                {
                    stopAllActions();
                    return;
                }
                //
                string name = BalloonTypeName[(int)balloonType] + "_" + BalloonsNum;
                List<CCSpriteFrame> temp = m_framesDict[name];
                CCAnimation ani = CCAnimation.animationWithFrames(temp);
                CCAnimate action = CCAnimate.actionWithDuration(1.0f, ani, true);
                action.tag = anima_tag;
                //
                this.stopActionByTag(anima_tag);
                this.runAction(CCRepeatForever.actionWithAction(action));
            }
        }
        public bool Intersects(CollideCircle bulletKeypoint, bool justByCenter)
        {
            bool bHit = false;
            for (int i = 0; i < m_BoundingCircles.Count;++i )
            {
                CollideCircle c = m_BoundingCircles[i];
                
                if(justByCenter)
                {
                    bHit = c.Intersects(bulletKeypoint.center);
                }
                else
                {
                    bHit = c.Intersects(bulletKeypoint);
                }
                //
                if (bHit) return bHit;
            }
            //
            return bHit;
        }
    }
}
